package com.woniuxy.cinemaServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cinemaServer.dao.CinemaInfoMapper;
import com.woniuxy.cinemaServer.exception.cinema.CinemaException;
import com.woniuxy.cinemaServer.exception.cinema.CinemaExceptionCode;
import com.woniuxy.cinemaServer.exception.seat.SeatException;
import com.woniuxy.cinemaServer.exception.seat.SeatExceptionCode;
import com.woniuxy.cinemaServer.model.SeatInfo;
import com.woniuxy.cinemaServer.dao.SeatInfoMapper;
import com.woniuxy.cinemaServer.param.seatInfo.SeatChangeParam;
import com.woniuxy.cinemaServer.param.seatInfo.SeatLoadParam;
import com.woniuxy.cinemaServer.service.SeatInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cinemaServer.util.StateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年03月20日
 */
@Service
public class SeatInfoServiceImpl extends ServiceImpl<SeatInfoMapper, SeatInfo> implements SeatInfoService {
    @Autowired
    private SeatInfoMapper seatInfoMapper;
    @Autowired
    private CinemaInfoMapper cinemaInfoMapper;

    @Override
    public void del(Integer id) {
        SeatInfo seatInfo = seatInfoMapper.selectById(id);
        if (seatInfo==null) throw new SeatException(SeatExceptionCode.SEAT_NOT_EXIST);
        if (seatInfo.getSeatStatus()!=StateUtil.SEAT_STATE_NORMAL) throw new CinemaException(CinemaExceptionCode.CINEMA_STATE_ERROR);
        seatInfo.setSeatStatus(StateUtil.SEAT_STATE_DISABLED);
        seatInfoMapper.updateById(seatInfo);
    }

    @Override
    public void change(SeatChangeParam param) {
        SeatInfo seatInfo = seatInfoMapper.selectById(param.getId());
        if (seatInfo==null) throw new SeatException(SeatExceptionCode.SEAT_NOT_EXIST);
        if (seatInfo.getSeatStatus()==param.getState()) throw new SeatException(SeatExceptionCode.SEAT_STATE_ERROR);
        seatInfo.setSeatStatus(param.getState());
        seatInfoMapper.updateById(seatInfo);
    }

    @Override
    public List<SeatInfo> load(SeatLoadParam param) {
        QueryWrapper<SeatInfo> q = new QueryWrapper<>();
        if (param.getCinemaId()!=null) q.eq("cinema_id",param.getCinemaId());
        if (param.getSeatId()!=null) q.eq("seat_id",param.getSeatId());
        if (param.getState()!=null) q.eq("seat_status",param.getState());
        List<SeatInfo> seatInfos = seatInfoMapper.selectList(q);
        return seatInfos;
    }
}
